import axios from 'axios';
import { ElMessage } from 'element-plus';

axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";

// console.log(import.meta.env.VITE_APP_BASE_API)
const server = axios.create({
    //从环境变量中取公共路径
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 5000,
})
// 请求拦截
server.interceptors.request.use(config => {
    const token = localStorage.getItem("token");
    if (!config.headers.token && token) {
        config.headers['token'] = token
    }
    // config.headers.token = localStorage.getItem("token");
    // console.log(config)
    return config;
})
// 响应拦截
server.interceptors.response.use(res => {
    const code = res.data.code;
    if (code !== 200) {
        // ElMessage.error(res.data.message);
    }
    return res.data;
}, err => {
    let message = err.message;
    ElMessage.error(message);
});

export default server;